# !/usr/bin/env python
# -*-coding:utf-8 -*-
from django.db.models import Manager

from common.querysets import BaseQuerySet


class MyManager(Manager):

    def __init__(self, *args, **kwargs):
        self.__add_is_del_filter = True
        super(MyManager, self).__init__(*args, **kwargs)

    _queryset_class = BaseQuerySet

    def get_queryset(self, *args, **kwargs):

        queryset = super(MyManager, self).get_queryset(*args, **kwargs)

        # 判断用户是否主动传入is_del的筛选条件，如果主动传入，就不做自动筛选操作
        if self.__add_is_del_filter:
            # 过滤掉已删除的记录
            queryset = queryset.filter(is_deleted=False)

        return queryset

    def filter(self, *args, **kwargs):

        # 判断用户是否主动传入is_del的筛选条件，如果主动传入，就不做自动筛选操作
        if not kwargs.get('is_deleted') is None:
            self.__add_is_del_filter = False
        f = super(MyManager, self).filter(*args, **kwargs)
        return f

    def create(self, **kwargs):
        return super().create(**kwargs)


